import time
from selenium.webdriver.common.by import By
from common.base import Base
from common.base import open_browser

# 商品浏览目标网页
browsegoods_url = 'http://139.129.26.163/index.php'


class BrowseGoodsPage(Base):
    Home_page_loc = (By.LINK_TEXT, '首页')  # 首页定位
    Clothes_loc = (By.XPATH, "//*[@id='mainNav']/div/ul/li[2]/a")  # 服装定位
    Mobile_power_loc = (By.XPATH, "//*[@id='mainNav']/div/ul/li[3]/a")  # 移动电源定位
    Digital_fashion_loc = (By.XPATH, "//*[@id='mainNav']/div/ul/li[4]/a")  # 数码时尚定位
    Home_appliance_loc = (By.XPATH, "//*[@id='mainNav']/div/ul/li[5]/a")  # 家用电器定位
    Search_loc = (By.NAME, 'imageField')  # 搜索按钮定位
    All_goods_loc = (By.CSS_SELECTOR, "div.goodsItem>a>img")  # 所有商品定位

    # 点击首页
    def click_Home_page(self):
        self.click(self.Home_page_loc)

    # 点击服装
    def click_Clothes(self):
        self.click(self.Clothes_loc)

    # 点击移动电源
    def click_Mobile_power(self):
        self.click(self.Mobile_power_loc)

    # 点击数码时尚
    def click_Digital_fashion(self):
        self.click(self.Digital_fashion_loc)

    # 点击家用电器
    def click_Home_appliance(self):
        self.click(self.Home_appliance_loc)
        self.back()  # 返回

    # 点击搜索
    def click_Search(self):
        self.click(self.Search_loc)

    # 点击下一页
    def click_next_page(self):
        Next_page_loc = (By.LINK_TEXT, '下一页')
        self.click(Next_page_loc)


if __name__ == '__main__':
    goods = BrowseGoodsPage(open_browser())
    goods.get(browsegoods_url)
    goods.window()

    goods.click_Home_page()
    time.sleep(2)

    goods.click_Clothes()
    time.sleep(2)

    goods.back()
    time.sleep(2)

    goods.click_Mobile_power()
    time.sleep(2)
    goods.click_Digital_fashion()
    time.sleep(2)
    goods.click_Home_appliance()

    time.sleep(2)
    goods.click_Search()

    time.sleep(2)
    goods.click_next_page()

    goods.quit()
